package com.wenxixuanwu.fruitstore.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenxixuanwu.fruitstore.entity.Banner;
import com.wenxixuanwu.fruitstore.mapper.BackBannerMapper;
import com.wenxixuanwu.fruitstore.service.BackBannerService;
import com.wenxixuanwu.fruitstore.service.exception.BannerNotFundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/4/22
 */
@Service
public class BackBannerServiceImpl implements BackBannerService {

    @Autowired
    private BackBannerMapper bannerMapper;

    @Override
    public List<Banner> findBannerAll() {
        List<Banner> result = bannerMapper.findBannerAll();
        if (result == null){
            throw new BannerNotFundException("轮播图数据不存在！请联系管理员");
        }
        return  result;
    }

    @Override
    public Page<Banner> findByPageService(Integer pageCode, Integer pageSize) {
        //创建Page对象，传入两个参数；当前页和每页的显示记录数
        Page<Banner> page = new Page<>(pageCode,pageSize);
        //2.将分页查询到的数据封装到Page对象中
        bannerMapper.selectPage(page,null);
        return page;
    }

    @Override
    public Integer deleteBanner(Integer id) {
        return bannerMapper.deleteBanner(id);
    }

    @Override
    public Integer insertBanner(Banner banners) {
        Banner banner = new Banner();
        banner.setUrl(banners.getUrl());
        banner.setCategorys(banners.getCategorys());
        banner.setBCreatDate(new Date());
        Integer result = bannerMapper.insertBanner(banner);
        return result;
    }
}
